package PMCXMLParser;

import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * Title: Term Classifier
 *
 * Description: A toolkit to find the relevance of candidate terms from a text corpus
 * to any particular topic/sub-domain using a term classification driven approach. The
 * system utilizes the lexical and contextual profiles of the candidate and domain-representing
 * "Resource Terms" (Seed and Ontological). The lexical and contextual profiles of candidate terms
 * are compared with those of Resource Terms to measure their relevance to the topic of interest.
 *
 * Derive_Distinct_Sentences class converts the
 * <p>Company: The University of Manchester</p>
 *
 * @author Hammad Afzal
 * @version 1.0

 */
public class String_Manipulator_Impl 
{

    public String pattern_replacer(String str, String pat, String replacer)
    {
        Pattern pattern = Pattern.compile(pat);
        Matcher matcher = pattern.matcher(str);
        return matcher.replaceAll(replacer);
    }

    public String string_replacer(String str, String startPat, String endPat, String replacer)
    {
        int start = 0;
        int temp = 0;
        while((start = str.indexOf(startPat, start)) != -1)
        {
            temp = str.indexOf(endPat, start + startPat.length());
            String str1 = str.substring(0, start);
            String str2 = str.substring(temp + endPat.length(), str.length());

            str = str1 + replacer + str2;
            start = start + temp;
        }
        return str;
    }

    public String string_replacer_start_zero(String mystr, String startPat, String endPat, String replacer)
    {
        int start = 0;
        int temp = 0;
        while((start = mystr.indexOf(startPat, start)) != -1)
        {
            temp = mystr.indexOf(endPat, start + startPat.length());
            String str1 = mystr.substring(0, start);
            String str2 = mystr.substring(temp + endPat.length(), mystr.length());

            mystr = str1 + replacer + str2;
            start = 0;
        }
        return mystr;
    }

    /*
     * It retrieves the string value that appears between the starting pattern and
     * ending pattern. Staring pattern and ending pattern may be a character or group
     * of characters.
     * This function can be used to extract a value between tags (e.g. XML etc.)
     */
    public String string_extractor(String inputStr, String startPat, String endPat)
    {
        int index = 0;
        int temp = 0;

        index = inputStr.indexOf(startPat, index);
        index = index + startPat.length();
        temp = index;
        index = inputStr.indexOf(endPat, index);

        if(index != -1)
        {
            inputStr = inputStr.substring(temp, index);
        }
        else
        {
            inputStr = "";
        }
        return inputStr;
    }

    public String make_query_from_keywords(String keywords, String tokenSeperator)
    {
        StringBuffer strBuf = new StringBuffer();
        StringTokenizer st1 = new StringTokenizer(keywords, tokenSeperator);
        while(st1.hasMoreElements())
        {
            String strLink = st1.nextToken();
            strBuf.append(strLink);
            strBuf.append("+");
        }
        return strBuf.toString().substring(0, strBuf.length() - 1);
    }

    public String[] generalStringTokenizer(String keywords, String tokenSeperator)
    {
        String[] element = null;
        int n = 0;
        StringTokenizer st1 = new StringTokenizer(keywords, tokenSeperator);
        while(st1.hasMoreElements())
        {
            String strLink = st1.nextToken();
            element[n] = strLink;
            n ++;
        }
        return element;
    }

    public String replace_letter(String input, String tokenSeperator, String replacer)
    {
        StringBuffer strBuf = new StringBuffer();
        StringTokenizer st1 = new StringTokenizer(input, tokenSeperator);
        while(st1.hasMoreElements())
        {
            String strLink = st1.nextToken();
            strBuf.append(strLink);
            strBuf.append(replacer);
        }
        return strBuf.toString().substring(0, strBuf.length() - 1);
    }

        /**
     * Returns the file name without extension
     * @param input_string
     * @param extension
     * @param delimiter
     * @return
     */
    public String strip_string_at_delimiter(String input_string, String delimiter)
    {
        int delimiter_index = input_string.indexOf(delimiter);
        input_string = input_string.substring(0,delimiter_index);
        return input_string;
    }
    /**
     * Removes braces around the given String
     * E.g. Input  = [Str]
     *      Output = Str
     * @param str
     * @return
     */
    public String remove_braces_around_string(String str)
    {
        if ((str.startsWith("[")) && (str.endsWith("]")))
        {
            str = str.substring(1, str.length() - 1);
        }
        return str;
    }


}


